package controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import entity.AjaxResult;
import entity.Criteria;
import entity.Goods;
import entity.LayuiEntity;
import entity.MyCart;
import service.impl.MyCartServiceImpl;


@Controller
public class MyCartController {

	@Autowired
	private MyCartServiceImpl service;
	//首页数据加载
	
	@RequestMapping("insertMyCart")
	@ResponseBody
	public AjaxResult insertMyCart(MyCart cart){
		
		//创建返回对象
		AjaxResult ajax=new AjaxResult();
		try {
			//如果加入购物车成功  返回success
			service.insertMyCart(cart);
			ajax.setMsg("success");
		} catch (Exception e) {
			//如果失败， 返回fail
			e.printStackTrace();
			ajax.setMsg("fail");
		}
		return ajax;
	}
	
	
	//http://localhost:8080/xiaomi/showMyCart?uid=2&page=1
	@RequestMapping("showMyCart")
	@ResponseBody
	public LayuiEntity<MyCart> showMyCart(Integer uid,
										@RequestParam(defaultValue="1")Integer page){
		//通过page计算分页的start offset
		Integer start=(page-1)*5;
		Integer offset=5;
		//调用service查询
		List<MyCart> list=service.selectMyCart(uid, start, offset);
		//查询行数
		Integer count=service.getCount(uid);
		//包装返回对象
		LayuiEntity<MyCart> entity=new LayuiEntity<MyCart>();
		entity.setCount(count);
		entity.setData(list);
		return entity; 
	}
	
	
	@RequestMapping("checkoutCart")
	@ResponseBody
	public String checkoutCart(Integer uid){
		try {
			//调用service结算购物车
			service.checkOutCart(uid);
			//成功后返回success
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			//失败返回fail
			return "fail";
		}
	}
	
	
	@RequestMapping("getCartTotal")
	@ResponseBody
	public Double getCartTotal(Integer uid){
		return service.getCartTotalPrice(uid);
	}
}
